﻿Public Class CLabelCheckState : Inherits CStateBase

    ''' <summary>
    ''' 実行可能かを判断する
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function IsEnter(ByVal task As CTaskBase) As Boolean
        Debug.WriteLine("CLabelCheckState.IsEnter")
        If CCommon.isInStr(task.ActionCd, "ORDERTOLABEL", "LABEL") Then
            Return True
        Else
            Return False
        End If
    End Function

    ''' <summary>
    ''' 実行メソッド
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function DoExecute(ByVal task As CTaskBase) As CStateBase
        Dim nextState As CStateBase = Nothing
        Dim pdaTask As CPDATask = DirectCast(task, CPDATask)
        Debug.WriteLine("CLabelCheckState.DoExecute")


        If task.ActionCd = "ORDERTOLABEL" Then
            ''ラベル情報の初期化
            pdaTask.isReadShipOrderCd = True
            pdaTask.clrLabel()
        End If

        If pdaTask.isGroupReportCd Then
            ''統括レポートコード処理の場合
            Dim i As Integer
            For i = 0 To pdaTask.GroupReportCdList.Length - 1
                If Not pdaTask.GroupReportCdList(i).isCheckFlag Then
                    Exit For
                End If
            Next
            If i <= pdaTask.GroupReportCdList.Length - 1 Then
                task.ActionCd = "END"
                nextState = GetNextState(task)
                Return nextState
            Else

                ''統括レポートコード処理終了
                pdaTask.isGroupReportCd = False
                ''エラー情報一覧を表示する
                Dim msg As String = ""
                Dim msgCnt As Integer = 1
                For i = 0 To pdaTask.GroupReportCdList.Length - 1
                    If Not pdaTask.GroupReportCdList(i).isOkFlag Then
                        msg += "p." & pdaTask.GroupReportCdList(i).PageNo
                        If msgCnt Mod 3 = 0 Then
                            msg += vbNewLine
                        Else
                            msg += " "
                        End If
                        msgCnt += 1
                    End If
                Next

                ' ''統括レポートコード処理終了
                'pdaTask.isGroupReportCd = False
                ' ''エラー情報一覧を表示する
                'Dim msg As String = ""
                'For i = 0 To pdaTask.GroupReportCdList.Length - 1
                '    If Not pdaTask.GroupReportCdList(i).isOkFlag Then
                '        msg += "p." & pdaTask.GroupReportCdList(i).PageNo & vbNewLine
                '    End If
                'Next

                If msg <> "" Then
                    ''１枚でも異常があった場合はメッセージ
                    pdaTask.Hardware.alert()
                    Dim result As String = ""
                    Dim prevWindowMode As EnumWindowMode
                    Do While result <> "[OK]"
                        If result = "" Then
                            prevWindowMode = pdaTask.WindowManager.GetWindowMode()
                            pdaTask.WindowManager.SetAlertMessage(CCommon.CNS_MSG_051 & vbNewLine & msg, CCommon.COLOR_WHITE, CCommon.COLOR_RED)
                            pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.ALERT_MODE)
                        End If
                        result = task.ReadTextWait(-1)
                        'Debug.WriteLine("RESULT:{0},{1}", result, task.ProcessText)
                    Loop
                    pdaTask.WindowManager.ChangeWindowMode(prevWindowMode)
                Else
                    pdaTask.Hardware.nextA()
                End If
            End If

        End If

        Dim AppManager As CApplicationManager = CApplicationManager.GetInstance

        If Not pdaTask.isSendStartFlag And task.ActionCd = "LABEL" Then
            Dim autoSendTime As Integer = AppManager.GetAppSettings.AutoSendTime
            If autoSendTime <> 0 Then
                ''自動送信機能をONにする
                pdaTask.isSendStartFlag = True
                pdaTask.WindowManager.AutoSend()
            End If
        End If

        Do While task.ProcessText = ""
            '' リーダーからデータを取得する(待ち時間制限なし)
            Dim isInnerLabelCheck As Boolean = AppManager.GetAppSettings.IsInnerLabelCheck
            If Not pdaTask.C_SHIPORDER.checkBoxQty(pdaTask.labelList.IsInMazda, pdaTask.labelList.IsReadMazda, pdaTask.labelList.LabelType, isInnerLabelCheck) Then
                'Debug.WriteLine(CCommon.CNS_MSG_011)
                pdaTask.WindowManager.SetMessage(CCommon.CNS_MSG_011, CCommon.COLOR_BLACK, CCommon.COLOR_GREEN)
            Else
                pdaTask.Hardware.nextA()
                If isInnerLabelCheck Then
                    pdaTask.WindowManager.SetMessage(CCommon.CNS_MSG_026, CCommon.COLOR_BLACK, CCommon.COLOR_GREEN)
                Else
                    'Debug.WriteLine(CCommon.CNS_MSG_012)
                    pdaTask.WindowManager.SetMessage(CCommon.CNS_MSG_012, CCommon.COLOR_BLACK, CCommon.COLOR_GREEN)
                End If
            End If
            task.ProcessText = task.ReadTextWait(-1)
        Loop

        Dim isLabelCheck As Boolean = AppManager.GetAppSettings.IsLabelCheck ''ラベル照合の有無


        Select Case CBarcode.GetBarcodeType(task.ProcessText)
            Case EnumBarcodeType.OUTER_LABEL_CODE_M
                '' マツダラベルの場合
                'nextState = GetNextState(task, GetType(CLabelState))
                If isLabelCheck Then
                    ''マツダラベルの新旧チェック（新の場合のみANYStateへ遷移）
                    If CCommon.chkMazdaLabelType(CCommon.getMazdaOrderCd(task.ProcessText)) = "new" Then
                        task.ActionCd = "LABEL-ANY"
                    Else
                        Dim result As String = ""
                        Dim prevWindowMode As EnumWindowMode
                        Do While result <> "[OK]"
                            If result = "" Then
                                pdaTask.Hardware.alert()
                                prevWindowMode = pdaTask.WindowManager.GetWindowMode()
                                pdaTask.WindowManager.SetAlertMessage(CCommon.CNS_MSG_040, CCommon.COLOR_BLACK, CCommon.COLOR_WHITE)
                                pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.ALERT_MODE)
                                'Debug.WriteLine(CCommon.CNS_MSG_015)

                            End If
                            result = task.ReadTextWait(-1)
                            'Debug.WriteLine("RESULT:{0},{1}", result, task.ProcessText)
                        Loop
                        pdaTask.WindowManager.ChangeWindowMode(prevWindowMode)
                        task.ActionCd = "LABEL"
                        task.ProcessText = ""
                    End If
                Else
                    task.ActionCd = "LABEL-M"
                End If
            Case EnumBarcodeType.OUTER_LABEL_CODE_I
                '' INOACラベルの場合
                'nextState = GetNextState(task, GetType(CINOACLabelState))
                If isLabelCheck Then
                    task.ActionCd = "LABEL-ANY"
                Else
                    task.ActionCd = "LABEL-I"
                End If
                'Case EnumBarcodeType.INNER_LABEL_CODE
                '    '' 社内ラベルの場合
                '    'nextState = GetNextState(task, GetType(CLabelState))
                '    task.ActionCd = "LABEL-IN"
            Case EnumBarcodeType.OUTER_LABEL_CODE_D
                '' ダイキョーラベルの場合
                'nextState = GetNextState(task, GetType(CLabelState))
                If isLabelCheck Then
                    Dim result As String = ""
                    Dim prevWindowMode As EnumWindowMode
                    Do While result <> "[OK]"
                        If result = "" Then
                            pdaTask.Hardware.alert()
                            prevWindowMode = pdaTask.WindowManager.GetWindowMode()
                            pdaTask.WindowManager.SetAlertMessage(CCommon.CNS_MSG_039, CCommon.COLOR_BLACK, CCommon.COLOR_WHITE)
                            pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.ALERT_MODE)
                            'Debug.WriteLine(CCommon.CNS_MSG_015)

                        End If
                        result = task.ReadTextWait(-1)
                        'Debug.WriteLine("RESULT:{0},{1}", result, task.ProcessText)
                    Loop
                    pdaTask.WindowManager.ChangeWindowMode(prevWindowMode)
                    task.ActionCd = "LABEL"
                    task.ProcessText = ""
                Else
                    task.ActionCd = "LABEL-D"
                End If
            Case EnumBarcodeType.INNER_LABEL_CODE
                '' 社内ラベルの場合
                'nextState = GetNextState(task, GetType(CLabelState))
                If isLabelCheck Then
                    task.ActionCd = "LABEL-ANY"
                Else
                    task.ActionCd = "LABEL-IN"
                End If
            Case EnumBarcodeType.INNER_DELIVERY
                '' 社内納品書の場合
                'nextState = GetNextState(task, GetType(CLabelState))
                'If isLabelCheck Then
                '    task.ActionCd = "LABEL-ANY"
                'Else
                '    task.ActionCd = "LABEL-IN"
                'End If
                task.ActionCd = "LABEL-ID"
            Case EnumBarcodeType.END_LABEL_CODE
                ''ラベル読込終了
                pdaTask.commitLabel()
                Dim isInnerLabelCheck As Boolean = AppManager.GetAppSettings.IsInnerLabelCheck

                Dim result As String = ""
                If Not pdaTask.C_SHIPORDER.checkBoxQty(pdaTask.labelList.IsInMazda, pdaTask.labelList.IsReadMazda, pdaTask.labelList.LabelType, isInnerLabelCheck) Then
                    ''未完了の注文有り
                    ''アラート
                    Dim prevWindowMode As EnumWindowMode
                    Do While result <> "[YES]" And result <> "[NO]"
                        If result = "" Then
                            pdaTask.Hardware.alert()
                            prevWindowMode = pdaTask.WindowManager.GetWindowMode()
                            pdaTask.WindowManager.SetAlertMessage(CCommon.CNS_MSG_013, CCommon.COLOR_BLACK, CCommon.COLOR_WHITE)
                            pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.YESNO_MODE)
                            'Debug.WriteLine(CCommon.CNS_MSG_013)

                        End If
                        result = task.ReadTextWait(-1)
                        'Debug.WriteLine("RESULT:{0},{1}", result, task.ProcessText)
                    Loop
                    pdaTask.WindowManager.ChangeWindowMode(prevWindowMode)
                End If
                If result = "" Or result = "[YES]" Then
                    pdaTask.WindowManager.SetMessage(CCommon.CNS_MSG_037, CCommon.COLOR_BLACK, CCommon.COLOR_GREEN)
                    pdaTask.clearUniqueCd() ''ユニークコードのクリア
                    'pdaTask.addShipOrderList(pdaTask.C_SHIPORDER.getReportCdList(), False) ''出荷指示番号の登録

                    ''データ送信
                    'Dim Cweb As CWebManager = CWebManager.GetInstance
                    'Cweb.uploadList(pdaTask.getSendActionData())
                    SyncLock pdaTask.m_SendLockObject
                        pdaTask.isSendingFlag = True
                        Try

                            pdaTask.C_FileCommon.AddTrxShipOrder(pdaTask.getSendReportData())
                            ''ファイル圧縮してサーバーに送信
                            Dim sendTrx As CWebManager = CWebManager.GetInstance
                            sendTrx.ZipFileUpload("TRX_SHIPORDER")
                        Catch ex As Exception

                        End Try
                        Try
                            pdaTask.C_FileCommon.AddTrxUniqueCd(pdaTask.getSendUniqueCd())
                            Dim sendTrx2 As CWebManager = CWebManager.GetInstance
                            sendTrx2.ZipFileUpload("TRX_UNIQUE")
                        Catch ex As Exception

                        End Try

                        Try
                            pdaTask.C_FileCommon.AddTrxOrder(pdaTask.getSendActionData())
                            pdaTask.C_FileCommon.SendTrxOrder()
                        Catch ex As Exception

                        End Try


                    End SyncLock
                    'pdaTask.isSendingFlag = False''直後にアプリ終了するため、必要ない

                    

                    pdaTask.WindowManager.BaseRelease()
                    pdaTask.C_SHIPORDER.Release()

                    pdaTask.WindowManager.ClearActionLabel()

                    'pdaTask.C_SHIPORDER = Nothing

                    'pdaTask.ShipOrderInfo = Nothing
                    pdaTask.changeShipOrderVisibleAll(False)

                    pdaTask.OrderInfo = Nothing
                    'Select Case pdaTask.StateType
                    '    Case 3
                    '        task.ActionCd = "END"
                    '        task.ProcessText = ""
                    '    Case 4
                    'End Select

                    pdaTask.WindowManager.CloseWindow()

                Else
                    task.ActionCd = "LABEL"
                    task.ProcessText = ""
                End If
                'nextState = GetNextState(task, GetType(CShiporderState))
            Case EnumBarcodeType.SHIPORDER_CODE
                ''出荷指示コードが読まれた場合
                If task.ActionCd = "ORDERTOLABEL" And pdaTask.StateType = 3 Then
                    ''まだラベルが読込まれていない場合は出荷指示読込みへ移る
                    ''現在の照合情報は保持したまま

                    'pdaTask.addShipOrderList(pdaTask.C_SHIPORDER.getReportCdList(), True) ''出荷指示番号の登録
                    task.ActionCd = "END"
                    'task.ProcessText = ""

                Else
                    ''既にラベルが読まれている場合はアラート
                    Dim result As String = ""
                    Dim prevWindowMode As EnumWindowMode
                    Do While result <> "[OK]"
                        If result = "" Then
                            pdaTask.Hardware.alert()
                            prevWindowMode = pdaTask.WindowManager.GetWindowMode()
                            pdaTask.WindowManager.SetAlertMessage(CCommon.CNS_MSG_024, CCommon.COLOR_BLACK, CCommon.COLOR_WHITE)
                            pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.ALERT_MODE)
                            'Debug.WriteLine(CCommon.CNS_MSG_015)

                        End If
                        result = task.ReadTextWait(-1)
                        'Debug.WriteLine("RESULT:{0},{1}", result, task.ProcessText)
                    Loop
                    pdaTask.WindowManager.ChangeWindowMode(prevWindowMode)

                    task.ActionCd = "LABEL"
                    task.ProcessText = ""
                End If
            Case EnumBarcodeType.GROUP_REPORT_CODE
                ''出荷指示コードが読まれた場合
                If task.ActionCd = "ORDERTOLABEL" And pdaTask.StateType = 3 Then
                    ''まだラベルが読込まれていない場合は出荷指示読込みへ移る
                    ''現在の照合情報は保持したまま

                    'pdaTask.addShipOrderList(pdaTask.C_SHIPORDER.getReportCdList(), True) ''出荷指示番号の登録
                    task.ActionCd = "END"
                    'task.ProcessText = ""

                Else
                    ''既にラベルが読まれている場合はアラート
                    Dim result As String = ""
                    Dim prevWindowMode As EnumWindowMode
                    Do While result <> "[OK]"
                        If result = "" Then
                            pdaTask.Hardware.alert()
                            prevWindowMode = pdaTask.WindowManager.GetWindowMode()
                            pdaTask.WindowManager.SetAlertMessage(CCommon.CNS_MSG_024, CCommon.COLOR_BLACK, CCommon.COLOR_WHITE)
                            pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.ALERT_MODE)
                            'Debug.WriteLine(CCommon.CNS_MSG_015)

                        End If
                        result = task.ReadTextWait(-1)
                        'Debug.WriteLine("RESULT:{0},{1}", result, task.ProcessText)
                    Loop
                    pdaTask.WindowManager.ChangeWindowMode(prevWindowMode)

                    task.ActionCd = "LABEL"
                    task.ProcessText = ""
                End If
            Case EnumBarcodeType.ORDER_CODE
                ''注文番号を読み取った場合アラート
                Dim result As String = ""
                Dim prevWindowMode As EnumWindowMode
                Do While result <> "[OK]"
                    If result = "" Then
                        pdaTask.Hardware.alert()
                        prevWindowMode = pdaTask.WindowManager.GetWindowMode()
                        pdaTask.WindowManager.SetAlertMessage(CCommon.CNS_MSG_027, CCommon.COLOR_BLACK, CCommon.COLOR_WHITE)
                        pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.ALERT_MODE)
                        'Debug.WriteLine(CCommon.CNS_MSG_015)

                    End If
                    result = task.ReadTextWait(-1)
                    'Debug.WriteLine("RESULT:{0},{1}", result, task.ProcessText)
                Loop
                pdaTask.WindowManager.ChangeWindowMode(prevWindowMode)

                task.ActionCd = "LABEL"
                task.ProcessText = ""

            Case EnumBarcodeType.EMP_CODE

                Dim isStaticReadEmpCd As Boolean = AppManager.GetAppSettings.IsStaticReadEmpCd

                If isStaticReadEmpCd Then
                    ''従業員番号を読み取った場合アラート
                    Dim result As String = ""
                    Dim prevWindowMode As EnumWindowMode
                    Do While result <> "[OK]"
                        If result = "" Then
                            pdaTask.Hardware.alert()
                            prevWindowMode = pdaTask.WindowManager.GetWindowMode()
                            pdaTask.WindowManager.SetAlertMessage(CCommon.CNS_MSG_028, CCommon.COLOR_BLACK, CCommon.COLOR_WHITE)
                            pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.ALERT_MODE)
                            'Debug.WriteLine(CCommon.CNS_MSG_015)

                        End If
                        result = task.ReadTextWait(-1)
                        'Debug.WriteLine("RESULT:{0},{1}", result, task.ProcessText)
                    Loop
                    pdaTask.WindowManager.ChangeWindowMode(prevWindowMode)
                Else
                    ''名札自由読込
                    ''交代
                    Dim empDAO As CEmpDAO = CEmpDAO.GetInstance()
                    Dim empData As strctEmpInfo = empDAO.GetEmpInfo(task.ProcessText)
                    pdaTask.EmpInfo.empNo = empData.EmpCd
                    pdaTask.EmpInfo.empName = empData.EmpName
                    pdaTask.WindowManager.SetEmpInfo(pdaTask.EmpInfo.empNo & ":[" & pdaTask.EmpInfo.empName & "]")

                End If
                task.ActionCd = "LABEL"
                task.ProcessText = ""
            Case EnumBarcodeType.IRREGULAR_CODE
                ''各種コード
                task.ActionCd = "IRREGULAR"
            Case Else
                '' それ以外のラベルが読まれた場合 -> エラーとする
                pdaTask.Hardware.warning()
                pdaTask.rollbackLabel()
                SyncLock pdaTask.m_SendLockObject
                    Try
                        pdaTask.C_FileCommon.AddTrxOrder(pdaTask.getSendErrorData(task.ProcessText, 0, "", "", "", "", 0))
                        pdaTask.C_FileCommon.SendTrxOrder()

                    Catch ex As Exception

                    End Try
                End SyncLock


                Dim result As String = ""
                Dim prevWindowMode As EnumWindowMode
                Do While result <> "[OK]"
                    If result = "" Then
                        prevWindowMode = pdaTask.WindowManager.GetWindowMode()
                        pdaTask.WindowManager.SetAlertMessage(CCommon.CNS_MSG_014, CCommon.COLOR_WHITE, CCommon.COLOR_RED)
                        pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.ALERT_MODE)
                    End If
                    result = task.ReadTextWait(-1)
                    'Debug.WriteLine("RESULT:{0},{1}", result, task.ProcessText)
                Loop
                pdaTask.WindowManager.ChangeWindowMode(prevWindowMode)


                'task.CommitState(Me)    '' コミットポイント作成
                task.ProcessText = ""
                task.ErrorCd = "EN"
                nextState = GetNextState(task, GetType(CErrorState))
                Return nextState
        End Select

        nextState = GetNextState(task)
        Return nextState
    End Function

    ''' <summary>
    ''' 終了メソッド(未使用)
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function DoExit(ByVal task As CTaskBase) As Boolean
        Debug.WriteLine("CLabelCheckState.DoExit")
        Return True
    End Function


End Class
